import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Set;

public class IO {

    public static void exeCmd(String in,String out){
        //输入文件inputF
        File inPutF = new File(in);
        try {
            //将标准输出重定向到输出文件中
            PrintStream ps = new PrintStream(out);
            System.setOut(ps);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        //判断指令的正确性
        //获取文件内容，得到指令集
        ArrayList<String> strC = getTxt(inPutF);
        //对指令集进行遍历
        for (String tmpStr : strC) {
            //筛查过短指令，同时防止.charAt()函数越界
            if (tmpStr.length() < 9) {
                System.out.println("error\n-----");
            }
            //输出选手信息
            else if (tmpStr.startsWith("players", 2)) {
                Players.showPlayers();
            }
            //输出对应的比赛结果
            else if (tmpStr.startsWith("result", 2) && tmpStr.charAt(8) == ' ') {
                //比赛名称集合
                Set<String> competitions = Set.of("women 1m springboard","women 3m springboard","women 10m platform",
                        "women 3m synchronised","women 10m synchronised","men 1m springboard","men 3m springboard",
                        "men 10m platform","men 3m synchronised","men 10m synchronised");
                //获取比赛名称
                String competitionName = tmpStr.substring(9);
                //是否需要输出详细信息
                boolean needDetail = competitionName
                        .startsWith("detail",competitionName.length()-"detail".length());

                //输出决赛对应运动项目结果
                if (!needDetail && competitions.contains(competitionName)) {
                    //todo:对.showOffResult()函数进行修改
                    Result.showOffResult(competitionName);
                }
                //输出详细结果
                else if(needDetail && competitions
                        .contains(competitionName.substring(0, competitionName.length()-" detail".length()))){
                    //todo:对detail需求进行开发
                    System.out.println("todo");
                }
                //比赛名称错误，或不存在对应比赛
                else {
                    System.out.println("N/A\n-----");
                }
            }
            //指令错误，或不存在对应指令
            else {
                System.out.println("error\n-----");
            }
        }
    }

    //读取input文本中的内容
    //输入：input文件
    //输出：文件内容 String字符串数组
    public static ArrayList<String> getTxt(File file) {
        ArrayList<String> command = new ArrayList<>();
        try {
            // 构造一个BufferedReader类来读取文件
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s;
            // 使用readLine方法，一次读一行
            while ((s = br.readLine()) != null) {
                //在字符串前插入行分隔符
                command.add(System.lineSeparator() + s);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return command;
    }

    //从指定 JSON 文件中读取内容
    //输入：json文件名
    //输出：json文件内容字符串
    public static String getStr(File jsonFile) {
        String jsonStr;
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
            //用于临时存储读取到的字符
            int ch;
            StringBuilder sb = new StringBuilder();

            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }

            fileReader.close();
            reader.close();

            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
